package com.jwdf.uda.ui.adapter.viewholder;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.IncludeCommentUserInfoBinding;
import com.jwdf.uda.databinding.ItemCommentTxtBinding;
import com.jwdf.uda.event.GuideDetailItemClickEvent;
import com.jwdf.uda.ui.activity.UserInfoActivity;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.ui.adapter.item.GuideDetailItem;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.UdaUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 类说明：
 */
public class GuideCommentTxtViewHolder extends AbsViewHolder<GuideDetailItem> {
    private ItemCommentTxtBinding mItemBinding;
    public GuideCommentTxtViewHolder(View convertView) {
        super(convertView);
        mItemBinding = DataBindingUtil.bind(convertView);
        convertView.setOnClickListener(this);
        mItemBinding.topLayout.llHeadLayout.setOnClickListener(this);
    }
    @Override
    protected void onBindView(GuideDetailItem item) {
        PbBean.PbBaseComment comment = item.comment;
        if(comment == null)
            return;
        mItemBinding.setIsDesigner(comment.getUserInfo().getIsDesigner());
        mItemBinding.tvTime.setVisibility(View.VISIBLE);
        mItemBinding.tvTime.setText(DateUtils.getFriendlyTime(comment.getCreateTime()));
        onBindCommonView(mItemBinding.topLayout, comment);
    }
    static void onBindCommonView(IncludeCommentUserInfoBinding topLayout, PbBean.PbBaseComment item) {
        topLayout.tvName.setText(item.getUserInfo().getNickname());
        topLayout.ivIsDesigner.setVisibility(item.getUserInfo().getIsDesigner()?View.VISIBLE:View.GONE);
        if(StringUtils.isNullEmpty(item.getContent())){
            topLayout.tvContent.setVisibility(View.GONE);
        }else{
            topLayout.tvContent.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(item.getAtUserNickname())) {
                topLayout.tvContent.setText(topLayout.ivHeader.getContext().getString(R.string.item_comment_at,
                        item.getAtUserNickname(), UdaUtils.filterAtUserString(item.getContent())));
            } else {
                topLayout.tvContent.setText(UdaUtils.filterAtUserString(item.getContent()));
            }
        }
        topLayout.tvDate.setVisibility(TextUtils.isEmpty(StringUtils.filterNullAndTrim(item.getUserInfo().getIntroduction()))?View.GONE:View.VISIBLE);
        topLayout.tvDate.setText(item.getUserInfo().getIntroduction());
        ImageLoader.getInstance().displayImage(item.getUserInfo().getAvatar().getUrl(), topLayout.ivHeader);
    }
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.ll_head_layout){
            if(item.comment != null){
                UserInfoActivity.start(v.getContext(), item.comment.getUserInfo().getUserId(), item.comment.getUserInfo().getIsDesigner());
            }
            return;
        }
        EventBusUtil.post(new GuideDetailItemClickEvent(item));
    }
}
